package com.binance.notification.web.push;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import cn.jiguang.common.resp.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpResponseParser {

    public static void parser(HttpResponse response, ResponseWrapper wrapper, String requestBodyContent) {
        if (response == null) {
            StringBuilder sb = new StringBuilder();
            String regex = "\"sendno\":([0-9])";
            Matcher m = Pattern.compile(regex).matcher(requestBodyContent);
            while (m.find()) {
                String sendno = m.group(0);
                sb.append("{");
                sb.append(sendno);
                sb.append("},");
            }
            wrapper.responseCode = 200;
            wrapper.responseContent = "[" + sb.subSequence(0, sb.length() - 1).toString() + "]";
            return;
        }
        HttpEntity entity = response.getEntity();
        int status = response.getStatusLine().getStatusCode();
        String responseContent = null;
        try {
            responseContent = EntityUtils.toString(entity, "utf-8");
            EntityUtils.consume(entity);
        } catch (ParseException | IOException e) {
            log.error("消费response异常：{}", e);
        } finally {
            if (response instanceof CloseableHttpResponse) {
                CloseableHttpResponse resp = (CloseableHttpResponse) response;
                try {
                    resp.close();
                } catch (IOException e) {
                    log.error("关闭response异常：{}", e);
                }
            }
        }
        wrapper.responseCode = status;
        wrapper.responseContent = responseContent;
        log.debug(wrapper.responseContent);
        if (status >= 200 && status < 300) {
            log.debug("Succeed to get response OK - responseCode:" + status);
            log.debug("Response Content - " + responseContent);
        } else if (status >= 300 && status < 400) {
            log.warn(
                    "Normal response but unexpected - responseCode:" + status + ", responseContent:" + responseContent);
        } else {
            log.warn("Got error response - responseCode:" + status + ", responseContent:" + responseContent);
            switch (status) {
                case 400:
                    log.error("Your request params is invalid. Please check them according to error message.");
                    wrapper.setErrorObject();
                    break;
                case 401:
                    log.error("Authentication failed! Please check authentication params according to docs.");
                    wrapper.setErrorObject();
                    break;
                case 403:
                    log.error(
                            "Request is forbidden! Maybe your appkey is listed in blacklist or your params is invalid.");
                    wrapper.setErrorObject();
                    break;
                case 404:
                    log.error("Request page is not found! Maybe your params is invalid.");
                    wrapper.setErrorObject();
                    break;
                case 410:
                    log.error(
                            "Request resource is no longer in service. Please according to notice on official website.");
                    wrapper.setErrorObject();
                case 429:
                    log.error("Too many requests! Please review your appkey's request quota.");
                    wrapper.setErrorObject();
                    break;
                case 500:
                case 502:
                case 503:
                case 504:
                    log.error("Seems encountered server error. Maybe JPush is in maintenance? Please retry later.");
                    wrapper.setErrorObject();
                    break;
                default:
                    log.error("Unexpected response.");
            }
        }
    }

}
